<template>
  <div class="table-box">
    <ProTable
      title="区域列表"
      ref="proTableRef"
      row-key="id"
      :show-pagination="pageable"
      :columns="columns"
      :expand-row-keys="expandKeys"
      highlight-current-row
      :request-api="loadData">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <ElButton type="primary" :icon="CirclePlus" @click="onAddClicked">新增</ElButton>
        <ElButton type="primary" :icon="Expand" @click="onExpandClicked">展开全部</ElButton>
        <ElButton type="primary" :icon="Fold" @click="onFoldClicked">收起全部</ElButton>
      </template>

      <!-- 表格操作 -->
      <template #operation="scope">
        <ElButton type="primary" link :icon="View" @click="onViewClicked(scope.row)">查看</ElButton>
        <ElButton v-if="canAdd(scope.row)" type="primary" link :icon="Plus" @click="onAddClicked(scope.row)">新增</ElButton>
        <ElButton type="primary" link :icon="EditPen" @click="onEditClicked(scope.row)">编辑</ElButton>
        <ElButton type="primary" link :icon="Delete" @click="onDeleteClicked(scope.row)">删除</ElButton>
      </template>
    </ProTable>
    <AreaForm ref="formRef" @success="refresh" />
  </div>
</template>

<script setup lang="ts" name="ZLArea">
import { ref, unref, computed } from 'vue'
import ProTable from '@/components/ProTable/index.vue'
import { ColumnProps, ProTableInstance } from '@/components/ProTable/interface'
import { Delete, CirclePlus, EditPen, Plus, View, Fold, Expand } from '@element-plus/icons-vue'
import AreaForm from './area-form.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Area } from '@api/uc/area/interface'
import { removeArea, listAreaTable } from '@api/uc/area'
import { FormType } from '@/enums'
const proTableRef = ref<ProTableInstance>()
const expandSet = ref(new Set())
// 打开 drawer(新增、查看、编辑)
const formRef = ref<InstanceType<typeof AreaForm> | null>()
// 可分页状态
const pageable = ref(false)
// 表格配置项
const columns: ColumnProps[] = [
  { prop: 'name', label: '区域名称', align: 'left', search: { el: 'input', key: 'name' } },
  { prop: 'id', label: '区域编码', align: 'left', tag: true, search: { el: 'input', key: 'code' } },
  { prop: 'data.level', label: '级别', tag: true, search: { el: 'input', key: 'level' } },
  { prop: 'weight', label: '排序', tag: true },
  { prop: 'operation', label: '操作', fixed: 'right', width: 330 }
]

// 展开行数组
const expandKeys = computed(() => {
  return Array.from(expandSet.value)
})

function canAdd(row?: any) {
  const id = row.id
  return id.length < 19
}

// 加载数据
function loadData(query: Area.Query) {
  pageable.value = !!(query.code || query.name || query.level)
  query.pageable = unref(pageable)
  return listAreaTable(query)
}

// 刷新列表
function refresh() {
  proTableRef.value?.getTableList()
}

// 添加
const onAddClicked = (row?: any) => {
  let parentId = '0'
  if (row && row.id) {
    parentId = row.id
  }
  formRef.value?.open(FormType.ADD, { parentId })
}

// 展开
function onExpandClicked() {
  const data = proTableRef.value?.tableData
  expandSet.value = new Set()
  if (data) {
    toggleExpand(data)
  }
}

// 展开方法递归
function toggleExpand(data: any[]) {
  data.forEach(item => {
    expandSet.value.add(item.id)
    if (item.children) {
      toggleExpand(item.children)
    }
  })
}

// 收起
function onFoldClicked() {
  expandSet.value = new Set()
}

// 查看
const onViewClicked = (row: any) => {
  formRef.value?.open(FormType.VIEW, { id: row.id })
}

// 编辑
const onEditClicked = (row: any) => {
  formRef.value?.open(FormType.EDIT, { id: row.id })
}

// 删除
const onDeleteClicked = (row: any) => {
  ElMessageBox.confirm('您确定要删除该项吗?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      removeArea(row.id).then(() => {
        ElMessage({
          type: 'success',
          message: '删除成功!'
        })
        proTableRef.value?.getTableList()
      })
    })
    .catch(() => {
      ElMessage({
        type: 'warning',
        message: '删除取消!'
      })
    })
}
</script>
